/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.query.IsfdLoadExperimentQueryBo;
import com.comac.ins.isfd.domain.vo.GridFsTreeNodeVo;
import com.comac.ins.isfd.domain.vo.IsfdLoadExperimentVo;
import com.comac.ins.isfd.service.IIsfdLoadExperimentService;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 载荷试验任务
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/loadExperiment")
public class IsfdLoadExperimentController extends BaseController {

    @Autowired
    private IIsfdLoadExperimentService isfdLoadExperimentService;

    /**
     * 查询载荷试验任务列表
     */
    @SaCheckPermission("isfd:loadExperiment:list")
    @PostMapping("/list")
    public TableDataInfo<IsfdLoadExperimentVo> list(@RequestBody IsfdLoadExperimentQueryBo queryBo) {
        return isfdLoadExperimentService.queryPageList(queryBo);
    }


    /**
     * 载荷试验任务详情查询
     */
    @SaCheckPermission("isfd:loadExperiment:query")
    @GetMapping("/{id}")
    public R<IsfdLoadExperimentVo> getInfo(@NotNull(message = "主键不能为空")
                                           @PathVariable Long id) {
        return R.ok(isfdLoadExperimentService.queryById(id));
    }


    /**
     * 删除载荷试验任务
     */
    @SaCheckPermission("isfd:loadExperiment:remove")
    @Log(title = "载荷试验任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(isfdLoadExperimentService.deleteWithValidByIds(List.of(ids)));
    }

    /**
     * 查询试验任务结果文件列表（不分页）
     */
    @SaCheckPermission("business:loadExperiment:queryFileList")
    @GetMapping("/queryFileList")
    public R<GridFsTreeNodeVo> queryFileList(@RequestParam("loadExperimentId") Long loadExperimentId,
                                             @RequestParam(value = "fileName", required = false) String fileName) {
        return R.ok(isfdLoadExperimentService.queryFileList(loadExperimentId, fileName));
    }

    /**
     * 下载文件
     */
    @SaCheckPermission("business:loadExperiment:queryFileList")
    @PostMapping("/downloadFiles")
    public ResponseEntity<InputStreamResource> downloadFiles(@RequestBody List<String> ids) {
        return isfdLoadExperimentService.downloadFiles(ids);
    }
}
